home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / pctchnqs / 1991 / number4 / scantest.c < prev    next >
Text File  |  1991-08-01  |  1KB  |  46 lines

  1. #include <stdio.h>
  2.  
  3. #define FALSE 0
  4. #define TRUE !FALSE
  5. #define RECORDSIZE 33
  6.  
  7. main()
  8. {
  9.     FILE    *inFile;
  10.     char    name[26], sex, buf[RECORDSIZE + 1];
  11.     int     age = 0, totAge = 0, totRecs = 0, length;
  12.     double  gpa;
  13.     float   totGPA = 0.0;
  14.     int scanfix(char *, char *, ...);
  15.  
  16.     /* open the input data file */
  17.     inFile = fopen("students.dat", "rt");
  18.     if (inFile == NULL) {
  19.         fprintf(stderr, "Cannot open input file\n");
  20.         exit(1);
  21.     }
  22.  
  23.     /* process each record */
  24.     printf("Name                     Sex Age GPA\n\n");
  25.     while (fgets(buf, RECORDSIZE + 1, inFile) != NULL) {
  26.         if ((length = strlen(buf)) != RECORDSIZE) {
  27.            printf("Invalid record length (%d)\n", length);
  28.            exit(1);
  29.         }
  30.         scanfix(buf, "%-25s %1c %3d %3lf",
  31.             name, &sex, &age, &gpa);
  32.         printf("%-25s %c  %3d %3.1f\n",
  33.             name, sex, age, gpa);
  34.  
  35.         totRecs++;
  36.         totAge += age;
  37.         totGPA += gpa;
  38.     }
  39.  
  40.     fclose(inFile);
  41.     printf("\nNumber of records = %3d\n", totRecs);
  42.     printf("Average age       = %5.1f\n",
  43.         (float) totAge / totRecs);
  44.     printf("Average gpa       = %5.1f\n", totGPA / totRecs);
  45. }
  46.